<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateIndexSlideshowRequest;
use App\Http\Requests\UpdateIndexSlideshowRequest;
use App\Repositories\IndexSlideshowRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\PicturesRepository;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class IndexSlideshowController extends AppBaseController
{
    /** @var  IndexSlideshowRepository */
    private $indexSlideshowRepository;
    private $pic;

    public function __construct(IndexSlideshowRepository $indexSlideshowRepo, PicturesRepository $picturesRepository)
    {
        $this->indexSlideshowRepository = $indexSlideshowRepo;
        $this->pic = $picturesRepository;
    }

    /**
     * Display a listing of the IndexSlideshow.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->indexSlideshowRepository->pushCriteria(new RequestCriteria($request));
        $indexSlideshows = $this->indexSlideshowRepository->paginate(20);

        return view('index_slideshows.index')
            ->with('indexSlideshows', $indexSlideshows);
    }

    /**
     * Show the form for creating a new IndexSlideshow.
     *
     * @return Response
     */
    public function create()
    {
        return view('index_slideshows.create');
    }

    /**
     * Store a newly created IndexSlideshow in storage.
     *
     * @param CreateIndexSlideshowRequest $request
     *
     * @return Response
     */
    public function store(CreateIndexSlideshowRequest $request)
    {
        $input = $request->all();

        $_image = $request->file('pic');
        if(!empty($_image)){
            $_res = $this->pic->uploadByHash($_image,"colors",null,null);
            $input['pic'] = $_res->url;
        }else{
            unset($input['pic']);
        };
        $indexSlideshow = $this->indexSlideshowRepository->create($input);

        Flash::success('Index Slideshow saved successfully.');

        return redirect(route('indexSlideshows.index'));
    }

    /**
     * Display the specified IndexSlideshow.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $indexSlideshow = $this->indexSlideshowRepository->findWithoutFail($id);

        if (empty($indexSlideshow)) {
            Flash::error('Index Slideshow not found');

            return redirect(route('indexSlideshows.index'));
        }

        return view('index_slideshows.show')->with('indexSlideshow', $indexSlideshow);
    }

    /**
     * Show the form for editing the specified IndexSlideshow.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $indexSlideshow = $this->indexSlideshowRepository->findWithoutFail($id);

        if (empty($indexSlideshow)) {
            Flash::error('Index Slideshow not found');

            return redirect(route('indexSlideshows.index'));
        }

        return view('index_slideshows.edit')->with('indexSlideshow', $indexSlideshow);
    }

    /**
     * Update the specified IndexSlideshow in storage.
     *
     * @param  int              $id
     * @param UpdateIndexSlideshowRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateIndexSlideshowRequest $request)
    {
        $indexSlideshow = $this->indexSlideshowRepository->findWithoutFail($id);

        if (empty($indexSlideshow)) {
            Flash::error('Index Slideshow not found');

            return redirect(route('indexSlideshows.index'));
        }
        $input = $request->all();
        $_image = $request->file('pic');
        if(!empty($_image)){
            $_res = $this->pic->uploadByHash($_image,"colors",null,null);
            $input['pic'] = $_res->url;
        }else{
            unset($input['pic']);
        };
        $indexSlideshow = $this->indexSlideshowRepository->update($input, $id);

        Flash::success('Index Slideshow updated successfully.');

        return redirect(route('indexSlideshows.index'));
    }

    /**
     * Remove the specified IndexSlideshow from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $indexSlideshow = $this->indexSlideshowRepository->findWithoutFail($id);

        if (empty($indexSlideshow)) {
            Flash::error('Index Slideshow not found');

            return redirect(route('indexSlideshows.index'));
        }

        $this->indexSlideshowRepository->delete($id);

        Flash::success('Index Slideshow deleted successfully.');

        return redirect(route('indexSlideshows.index'));
    }
}
